/*
 * SearchPanel.java
 *
 * Created on 1.6.2009, 15:22:04
 */

package org.marvec.pisnickar.panels;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.lobobrowser.html.UserAgentContext;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.parser.DocumentBuilderImpl;
import org.lobobrowser.html.parser.InputSourceImpl;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.marvec.pisnickar.html.GlobalHtmlListener;
import org.marvec.pisnickar.html.HtmlListener;
import org.marvec.pisnickar.html.LocalHtmlRendererContext;
import org.marvec.pisnickar.html.SearchPageFactory;
import org.marvec.pisnickar.songs.SearchResult;
import org.marvec.pisnickar.songs.Song;
import org.marvec.pisnickar.songs.SongSource;
import org.marvec.pisnickar.songs.SourceManager;
import org.marvec.pisnickar.tabs.TabManipulator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author marvec
 */
public class SearchPanel extends javax.swing.JPanel {

    protected SourceManager sources;
    protected List<SearchResult> results;
    protected HtmlListener listener;
    private TabManipulator manipulator;
    private String query = "";

    /** Creates new form SearchPanel */
    public SearchPanel(SourceManager sources, TabManipulator manipulator, HtmlListener listener, String query, List<SearchResult> results) {
        this.query = query;
        this.sources = sources;
        this.results = results;
        this.listener = listener;
        this.manipulator = manipulator;

        initComponents();
        initHtmlPanel(htmlPanel1, listener, results);
    }

    protected Reader getResource(List<SearchResult> results) throws UnsupportedEncodingException {
        return new StringReader(SearchPageFactory.getSerachResultsAsHtml(query, results));
    }

    protected void initHtmlPanel(HtmlPanel panel, HtmlListener listener, List<SearchResult> results) {
        if (results != null) {
            try {
                UserAgentContext ucontext = new SimpleUserAgentContext();
                LocalHtmlRendererContext rcontext = new LocalHtmlRendererContext(panel, ucontext);
                rcontext.addHtmlListener(listener);
                rcontext.setSupressOriginalHanlder(true);
                DocumentBuilderImpl dbi = new DocumentBuilderImpl(ucontext, rcontext);
                Document document = dbi.parse(new InputSourceImpl(getResource(results), GlobalHtmlListener.INTERNAL_URL_PREFIX));
                panel.setDocument(document, rcontext);
            } catch (SAXException ex) {
                Logger.getLogger(SongPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(SongPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        htmlPanel1 = new org.lobobrowser.html.gui.HtmlPanel();
        jToolBar1 = new javax.swing.JToolBar();
        addToSelectionButton = new javax.swing.JButton();

        setName("Form"); // NOI18N

        htmlPanel1.setName("htmlPanel1"); // NOI18N

        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.marvec.pisnickar.PisnickarApp.class).getContext().getResourceMap(SearchPanel.class);
        addToSelectionButton.setText(resourceMap.getString("addToSelectionButton.text")); // NOI18N
        addToSelectionButton.setFocusable(false);
        addToSelectionButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addToSelectionButton.setName("addToSelectionButton"); // NOI18N
        addToSelectionButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        addToSelectionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addToSelectionButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(addToSelectionButton);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addComponent(htmlPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(htmlPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addToSelectionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addToSelectionButtonActionPerformed
        SongSource ss = sources.getDummySongSource();

        boolean wasError = false;
        for (SearchResult sr: results) {
            if (sr.getSource() != ss) {
                try {
                    ss.storeSong(null, (Song) sr.getSource().getSong(sr.getSongId()).clone());
                } catch (IOException ex) {
                    Logger.getLogger(SongPanel.class.getName()).log(Level.WARNING, null, ex);
                    wasError = true;
                }
            }
        }

        manipulator.refreshSelection();

        if (wasError) {
            JOptionPane.showMessageDialog(this, "Do výběru se nepodařilo vložit všechny písničky.",
                    "Chyba při vkládání do výběru", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_addToSelectionButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addToSelectionButton;
    private org.lobobrowser.html.gui.HtmlPanel htmlPanel1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

}
